package com.greenlaw110.cimple.controller;

import act.controller.Controller;
import com.greenlaw110.cimple.model.Article;
import com.greenlaw110.cimple.model.Cate;
import com.greenlaw110.cimple.util.StringUtils;
import com.jfinal.plugin.activerecord.Page;
import org.osgl.mvc.annotation.GetAction;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

import static act.controller.Controller.Util.*;

@Controller("/article")
public class ArticleController {

    @GetAction("")
    public void index(int pageNum) {
        if (0 == pageNum) {
            pageNum = 1;
        }
        String title = "文章管理";
        Page<Article> pageArticle = Article.getArticleByPage(pageNum);
        render(title, pageArticle);
    }

    /**
     * 发布文章页面跳转
     */
    @GetAction("/pub")
    public void pub() {
        List<Cate> cateLists = Cate.getAllCate();
        String title = "撰写";
        render(cateLists);
    }

    public void pubAction() {
        String tagStr = getPara("tags", "");
        int cateId = getParaToInt("cate", -1);
        String articleTitle = getPara("artTitle", "");
        String contents = getPara("contents", "");
        tagStr = StringUtils.contactTags(tagStr);

        Article art = new Article();
        Timestamp time = new Timestamp(System.currentTimeMillis());
        art.set("updateTime", time);
        art.set("title", articleTitle);
        art.set("tags", tagStr);
        art.set("content", contents);
        art.set("cateId", cateId);
        TransUtils utils = new TransUtils("myBlogTrans",
                "qPlGMEwV5mxG3diMGAHj7W+NZwhBuBVJ72svN7AuM6M=");
        String result;
        try {
            result = utils.translate(articleTitle, Language.ChineseSimplified,
                    Language.English);
            result = result.replace(",", "-").replace(" ", "-")
                    .replace("<", "").replace(">", "").replace("《", "")
                    .replace("》", "").replace("（", "").replace("）", "")
                    .replace("(", "").replace(")", "").replace("\"", "")
                    .replace("“", "").replace("”", "").trim();
            System.out.println(result);
            int transTitleLength = Article.checkTransTitle(result);
            if(transTitleLength > 0) {
                result += "-" + transTitleLength;
            }
            art.set("transTitle", result);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
        art.set("isDelete", 0);
        art.save();
        redirect("/admin/article");
    }


    public void del(){
        int artId = getParaToInt(0, -1);
        Article.dao.findById(artId).set("isDelete", 1).update();
        redirect("/admin/article");
    }

    public void modify(){
        int artId = getParaToInt(0, -1);
        setAttr("title", "修改");
        List<Cate> cateLists = Cate.getAllCate();
        setAttr("cateLists", cateLists);
        setAttr("art", Article.dao.findById(artId));
    }


    public void modifyAction(){
        int artId = getParaToInt("artId", -1);
        String tagStr = getPara("tags", "");
        int cateId = getParaToInt("cate", -1);
        String articleTitle = getPara("artTitle", "");
        String contents = getPara("contents", "");
        tagStr = StringUtils.contactTags(tagStr);

        Article art = Article.dao.findById(artId);
        art.set("title", articleTitle);
        art.set("tags", tagStr);
        art.set("content", contents);
        art.set("cateId", cateId);
        TransUtils utils = new TransUtils("myBlogTrans",
                "qPlGMEwV5mxG3diMGAHj7W+NZwhBuBVJ72svN7AuM6M=");
        String result;
        try {
            result = utils.translate(articleTitle, Language.ChineseSimplified,
                    Language.English);
            result = result.replace(",", "-").replace(" ", "-")
                    .replace("<", "").replace(">", "").replace("《", "")
                    .replace("》", "").replace("（", "").replace("）", "")
                    .replace("(", "").replace(")", "").replace("\"", "")
                    .replace("“", "").replace("”", "").trim();
            System.out.println(result);
            int transTitleLength = Article.checkTransTitle(result);
            if(transTitleLength > 0) {
                result += "-" + transTitleLength;
            }
            art.set("transTitle", result);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
        art.set("isDelete", 0);
        art.update();
        redirect("/admin/article");
    }


    public void holdOnline(){
        System.out.println("维持在线没有实际意义");
        renderText("");
    }
}